package com.cib.process.chat.web;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.cib.process.bean.EsbRequestServer;
import com.cib.process.bean.EsbResponseResult;
import com.cib.process.bean.IvrRequestServer;

import com.cib.process.bean.ServiceResult;
import com.cib.process.chat.service.IvrService;
import com.cib.process.util.LoggerHelper;
import org.apache.commons.lang.StringUtils;

import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import java.util.Map;

@RestController
@RequestMapping("/commonchat")
public class CommonChatController {
    @Autowired
    private IvrService ivrService;

//    produces = MediaType.APPLICATION_XML_VALUE+";charset=utf-8",consumes = MediaType.APPLICATION_XML_VALUE+";charset=uft-8"
    @RequestMapping(value = "/echo",method = RequestMethod.POST,produces = MediaType.APPLICATION_XML_VALUE,consumes = MediaType.APPLICATION_XML_VALUE)
    public EsbResponseResult commontChat(@RequestBody EsbRequestServer esbRequestServer){
        long startTime = System.currentTimeMillis();
        EsbRequestServer.Head serverHead = esbRequestServer.getHead();
        EsbRequestServer.ChatBody serverBody = esbRequestServer.getBody();
        try{
            IvrRequestServer ivrRequestServer=getIvrRequestServer(serverBody);
            LoggerHelper.info(this.getClass(),"[sessionId:{}] [userId:{}] chat request xml:{}",serverBody.getSessionId(),ivrRequestServer.getUserId(),esbRequestServer.toString());
            ServiceResult commonChat=ivrService.commonChat(ivrRequestServer);
            EsbResponseResult responseResult=getResponseResult(commonChat,serverHead);
            //..........log info 信息
            return responseResult;

        }catch (Exception e){
            LoggerHelper.err(this.getClass(),"common chat error",e);
           // return getErrorResult(serverHead);
        }

        return null;
    }

    private EsbResponseResult getResponseResult(ServiceResult commonChat, EsbRequestServer.Head serverHead) {
        EsbResponseResult esbResponseResult=new EsbResponseResult();
        EsbResponseResult.ResultHead resultHead=new EsbResponseResult.ResultHead();
        handleEsbHead(resultHead,serverHead);
        //.............封装错误码和错误信息等。
        esbResponseResult.setHead(resultHead);
        //。。。。。。。。。。。。
        //设置body
        EsbResponseResult.ChatResult chatResult=new EsbResponseResult.ChatResult();
        chatResult.setSessionId(commonChat.getSesisonId());
        chatResult.setNextAction(commonChat.getNextAction());
        Object nextResult = commonChat.getNextResult();
        if (nextResult!=null){
            chatResult.setNextResult(JSON.toJSONString(nextResult));
        }
        esbResponseResult.setBody(chatResult);
        return esbResponseResult;
    }

    private void handleEsbHead(EsbResponseResult.ResultHead resultHead, EsbRequestServer.Head serverHead) {
        resultHead.setSvcCd(serverHead.getSvcCd());
        resultHead.setSvcScn(serverHead.getSvcScn());
        //。。。。。。。。。。。。。。。。。。

    }

    private IvrRequestServer getIvrRequestServer(EsbRequestServer.ChatBody serverBody) {
        IvrRequestServer ivrRequestServer = new IvrRequestServer();
        ivrRequestServer.setSessionId(serverBody.getSessionId());
        ivrRequestServer.setLastAction(serverBody.getLastAction());
        ivrRequestServer.setToken(serverBody.getToken());

        //会话数据
        String sessionIdData = serverBody.getSessionIdData();
        if (StringUtils.isNotBlank(sessionIdData)){
            Map<String,Object> dataMap = JSON.parseObject(sessionIdData, Map.class);
            Object userId = dataMap.get("sjh");
            ivrRequestServer.setUserId(userId==null?"":userId.toString());
            Object syrhbz = dataMap.get("syrhbz");
            ivrRequestServer.setSryhbz(syrhbz==null?"":syrhbz.toString());
            ivrRequestServer.setSessionIdData(dataMap);
        }

        String lastResult=serverBody.getLastResult();
        if (StringUtils.isNotBlank(lastResult)){
            Map<String,String> lastResultMap = JSON.parseObject(lastResult, Map.class);
            String callaction = lastResultMap.get("callaction");
            ivrRequestServer.setVad(callaction);
            ivrRequestServer.setLastResult(lastResultMap);
        }

        return ivrRequestServer;
    }
}
